﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the post entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   | - Text
    ///   + Base
    ///    | - Topic(of the Primary level)
    ///    | - Parent(of the Primary level)
    ///    | - Member(of the Base level)
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class ForumPostEntity : AuditableEntity
    {
        #region Fields

        private ForumPostEntity _parent;
        private ICollection<ForumPostEntity> _posts;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public ForumPostEntity()
        {
            _parent = null;
            _posts = new List<ForumPostEntity>();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the parent post identifier.
        /// </summary>
        public virtual int ParentID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the parent post.
        /// </summary>
        public virtual ForumPostEntity Parent
        {
            get
            {
                return ParentID != 0 ? _parent : null;
            }
            set
            {
                _parent = value;
                ParentID = value != null ? value.EntityID : 0;
            }
        }

        /// <summary>
        /// Gets or sets the topic.
        /// </summary>
        public virtual ForumTopicEntity Topic
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the member.
        /// </summary>
        public virtual ForumMemberEntity Member
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        public virtual string Text
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual ForumPostFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Collections
        
        /// <summary>
        /// Gets or sets the child post collection.
        /// </summary>
        public virtual ICollection<ForumPostEntity> Childs
        {
            get
            {
                return _posts;
            }
            set
            {
                _posts = value;
            }
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level, 
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Topic"] = Topic.Disclose(DisclosureLevel.Primary, options);
                obj["Parent"] = Parent != null
                    ? Parent.Disclose(DisclosureLevel.Primary, options)
                    : null;
                obj["Member"] = Member.Disclose(DisclosureLevel.Base, options);
                obj["Flags"] = Flags.ToString();
            }
            if(level >= DisclosureLevel.Extended)
                obj["Text"] = Text;
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class ForumPostMapping : EntityMapping<ForumPostEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public ForumPostMapping()
        {
            Table("fwk_ForumPosts");
            Id(x => x.EntityID, m =>
            {
                m.Column("PostID");
                m.Generator(Generators.Identity);
            });
            Property(x => x.ParentID, m =>
            {
                m.Column("ParentPostID");
                m.NotNullable(true);
            });
            ManyToOne(x => x.Parent, m =>
            {
                m.Column("ParentPostID");
                m.NotFound(NotFoundMode.Ignore);
                m.Insert(false);
                m.Update(false);
            });
            ManyToOne(x => x.Topic, m =>
            {
                m.Column("TopicID");
                m.ForeignKey("FK_fwk_ForumPosts_fwk_ForumTopics");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            ManyToOne(x => x.Member, m =>
            {
                m.Column("MemberID");
                m.ForeignKey("FK_fwk_ForumPosts_fwk_ForumMembers");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            Property(x => x.Text, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
            Bag(x => x.Childs, m =>
            {
                m.Table("fwk_ForumPosts");
                m.Key(k =>
                {
                    k.Column("ParentPostID");
                    k.NotNullable(true);
                });
                m.Inverse(true);
                m.BatchSize(10);
            },
            a =>
            {
                a.OneToMany();
            });
        }
    }
}
